﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAL
{
    public class WeChatDAL
    {
        SQLHelper SQLHelper = null;
        QXHelper QXHelper = null;
        public WeChatDAL()
        {
            SQLHelper = new SQLHelper();
            QXHelper = new QXHelper();
        }


        #region 添加关键词
        public bool jhy_wechat_keywords_add(string kw_keyword,string kw_description, string kw_msgtype, string kw_visable)
        {
            bool flag = false;
            string proc = "jhy_wechat_keywords_add";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@kw_keyword",kw_keyword),
                new SqlParameter("@kw_description",kw_description),
                new SqlParameter("@kw_msgtype",kw_msgtype),
                new SqlParameter("@kw_visable",kw_visable)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }

        #endregion
        #region 修改关键词
        public bool jhy_wechat_keywords_edit(string kw_id, string kw_field, string kw_para)
        {
            bool flag = false;
            string proc = "jhy_wechat_keywords_edit";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@kw_id",kw_id),
                new SqlParameter("@kw_field",kw_field),
                new SqlParameter("@kw_para",kw_para)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
        #region 删除关键词
        public bool jhy_wechat_keywords_delete(string kw_id)
        {
            bool flag = false;
            string proc = "jhy_wechat_keywords_delete";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@kw_id",kw_id)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
        #region 查询关键词
        public string jhy_wechat_keywords_query(string kw_keyword)
        {
            DataTable dt = new DataTable();
            string proc = "jhy_wechat_keywords_query";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@kw_keyword",kw_keyword)
            };
            dt = SQLHelper.ExecuteQuery(proc, spa, CommandType.StoredProcedure);
            return QXHelper.LayuiTableJson(dt);
        }

        #endregion
        #region 添加关键词子项
        public bool jhy_wechat_keywords_item_add(string kw_parentid, string kw_news_title, string kw_news_description, string kw_news_picurl, string kw_news_url, string kw_text_content)
        {
            bool flag = false;
            string proc = "jhy_wechat_keywords_item_add";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@kw_parentid",kw_parentid),
                new SqlParameter("@kw_news_title",kw_news_title),
                new SqlParameter("@kw_news_description",kw_news_description),
                new SqlParameter("@kw_news_picurl",kw_news_picurl),
                new SqlParameter("@kw_news_url",kw_news_url),
                new SqlParameter("@kw_text_content",kw_text_content)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }

        #endregion
        #region 修改关键词子项
        public bool jhy_wechat_keywords_item_edit(string kw_id, string kw_field, string kw_para)
        {
            bool flag = false;
            string proc = "jhy_wechat_keywords_item_edit";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@kw_id",kw_id),
                new SqlParameter("@kw_field",kw_field),
                new SqlParameter("@kw_para",kw_para)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion
        #region 删除关键词子项
        public bool jhy_wechat_keywords_item_delete(string kw_id)
        {
            bool flag = false;
            string proc = "jhy_wechat_keywords_item_delete";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@kw_id",kw_id)
            };
            int res = SQLHelper.ExecuteNonQuery(proc, spa, CommandType.StoredProcedure);
            if (res > 0)
            {
                flag = true;
            }
            return flag;
        }
        #endregion        
        #region 查询关键词子项
        public string jhy_wechat_keywords_item_query(string kw_parentid)
        {
            DataTable dt = new DataTable();
            string proc = "jhy_wechat_keywords_item_query";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@kw_parentid",kw_parentid)
            };
            dt = SQLHelper.ExecuteQuery(proc, spa, CommandType.StoredProcedure);
            return QXHelper.LayuiTableJson(dt);
        }
        #endregion


        #region 关键词处理
        public DataTable jhy_wechat_handle_keywords_query(string keywords)
        {
            DataTable dt = new DataTable();
            WeChatHelper WH = new WeChatHelper();
            string proc = "jhy_wechat_handle_keywords_query";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@keywords",keywords)
            };
            dt = SQLHelper.ExecuteQuery(proc, spa, CommandType.StoredProcedure);
            return dt;
        }
        #endregion        
        #region 文章中检索微信关键词
        public string jhy_wechat_handle_article_query(string keywords)
        {
            DataTable dt = new DataTable();
            string proc = "jhy_wechat_handle_article_query";
            SqlParameter[] spa = new SqlParameter[] {
                new SqlParameter("@keywords",keywords)
            };
            dt = SQLHelper.ExecuteQuery(proc, spa, CommandType.StoredProcedure);
            return dt.Rows.Count.ToString();
        }
        #endregion
        #region 无关键词时返回菜单列表
        public string jhy_wechat_handle_keywords_null_query()
        {
            DataTable dt = new DataTable();
            string proc = "jhy_wechat_handle_keywords_null_query";
            dt = SQLHelper.ExecuteQuery(proc, CommandType.StoredProcedure);
            if (dt.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append("关键词:\n");
                foreach (DataRow dr in dt.Rows)
                {
                    sb.AppendFormat("[{0}] {1}\n", dr["kw_keyword"].ToString(), dr["kw_description"].ToString());
                }
                return sb.ToString()+ "输入对应关键词查看。";
            }
            else
            {
                return "管理员很懒，啥都没设置";
            }
        }
        #endregion
    }
}
